﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;

public partial class PageMaster : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            try
            {
                FillRole();
            }
            catch (Exception)
            {

                throw;
            }
        }
    }

    private void FillRole()
    {
        try
        {
            clsRoleDAL objclsRoleDAL = new clsRoleDAL();
            List<IN_TB_ROLE> objIN_TB_ROLE = new List<IN_TB_ROLE>();
            objIN_TB_ROLE = objclsRoleDAL.GetAllRole();
            ddlrole.Items.Clear();
            ddlrole.AppendDataBoundItems = true;
            ddlrole.DataTextField = "ROLE";
            ddlrole.DataValueField = "ID";
            ddlrole.Items.Add("Common");
            ddlrole.Items[0].Value = "0";
            ddlrole.DataSource = objIN_TB_ROLE;
            ddlrole.DataBind();
            FillPageBYRole(0);
            FillgrdPageByRole(0);
        }
        catch (Exception)
        {

            throw;
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        clsPageAccessDAL objclsPageAccessDAL = new clsPageAccessDAL();
        IN_TB_ROLE_PAGE objIN_TB_ROLE_PAGE = new IN_TB_ROLE_PAGE();
        objIN_TB_ROLE_PAGE.PAGE_ID = Convert.ToInt32(ddlpage.SelectedValue);
        objIN_TB_ROLE_PAGE.ROLEID = Convert.ToInt32(ddlrole.SelectedValue);
        int RolePageId = objclsPageAccessDAL.SaveRolePage(objIN_TB_ROLE_PAGE);
        FillPageBYRole(Convert.ToInt32(ddlrole.SelectedValue));
        FillgrdPageByRole(Convert.ToInt32(ddlrole.SelectedValue));
    }
    protected void ddlrole_SelectedIndexChanged(object sender, EventArgs e)
    {
        int RoleId = Convert.ToInt32(ddlrole.SelectedValue);
        FillPageBYRole(RoleId);
        FillgrdPageByRole(RoleId);
    }

    private void FillPageBYRole(int RoleId)
    {
        try
        {
            clsPageDAL objclsPageDAL = new clsPageDAL();
            List<IN_TB_PAGE_MST> objIN_TB_PAGE_MST = new List<IN_TB_PAGE_MST>();
            objIN_TB_PAGE_MST = objclsPageDAL.GetPageNotAssignedByRoleId(RoleId);
            ddlpage.Items.Clear();
            ddlpage.AppendDataBoundItems = true;
            ddlpage.DataTextField = "PAGE_NAME";
            ddlpage.DataValueField = "ID";
            ddlpage.Items.Add("Select");
            ddlpage.Items[0].Value = "0";
            ddlpage.DataSource = objIN_TB_PAGE_MST;
            ddlpage.DataBind();
        }
        catch (Exception)
        {

            throw;
        }

    }

    protected void FillgrdPageByRole(int RoleId)
    {
        try
        {
            clsPageDAL objclsPageDAL = new clsPageDAL();
            List<clsRolePageBo> objIN_TB_PAGE_MST = new List<clsRolePageBo>();
            objIN_TB_PAGE_MST = objclsPageDAL.GetRolePageByRoleId(RoleId);
            grdPage.DataSource = objIN_TB_PAGE_MST;
            grdPage.DataBind();
        }
        catch (Exception)
        {

            throw;
        }
    }

    protected void grdPage_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Del")
        {
            int RolePageId = Convert.ToInt32(e.CommandArgument.ToString());
            IN_TB_ROLE_PAGE objIN_TB_ROLE_PAGE = new IN_TB_ROLE_PAGE();
            clsPageAccessDAL objclsPageAccessDAL = new clsPageAccessDAL();
            objclsPageAccessDAL.DeleteRolePage(RolePageId);
            FillPageBYRole(Convert.ToInt32(ddlrole.SelectedValue));
            FillgrdPageByRole(Convert.ToInt32(ddlrole.SelectedValue));
        }
    }
}
